package com.mbyte.easy.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.security.service.IUserService;
import com.mbyte.easy.teacher.entity.Teacher;
import com.mbyte.easy.teacher.mapper.TeacherMapper;
import com.mbyte.easy.teacher.service.ITeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mbyte.easy.teacherInfo.entity.TTeacher;
import com.mbyte.easy.teacherInfo.mapper.TTeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lp
 * @since 2020-06-16
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements ITeacherService {
    @Autowired
    private TTeacherMapper tTeacherMapper;
    @Autowired
    private IUserService userService;
    @Override
    public IPage<Teacher> pageInfo(Page<Teacher> page, QueryWrapper<Teacher> queryWrapper, Teacher teacher) {
        IPage<Teacher> page1 = this.page(page, queryWrapper);
        List<Teacher> records = page1.getRecords();
        List<Teacher> result = new ArrayList<>();
        boolean isHave = false;
        for (int i = 0; i < records.size(); i++) {
            Teacher tTeacher1 = records.get(i);
            SysUser sysUser = userService.selectByPrimaryKey(tTeacher1.getUserId());
            tTeacher1.setUsername(sysUser.getUsername());
            tTeacher1.setSex(sysUser.getSexType());
            if(teacher.getUsername() != null && teacher.getUsername() != ""){
                isHave = true;
                if(teacher.getUsername().equals(tTeacher1.getUsername())){
                    result.add(tTeacher1);
                    page1.setRecords(result);
                    return page1;
                }
            }
        }
        if(isHave){
            page1.setRecords(result);
        }
        return page1;
    }
}
